package com.guodexian.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guodexian.dto.DishDto;
import com.guodexian.entity.Dish;
import com.guodexian.ex.BaseBusinessException;
import com.guodexian.ex.NameException;
import com.guodexian.mapper.DishMapper;
import com.guodexian.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 类说明：<br>
 *
 * @author qfrx<br>
 * 2024/12/26 19:01<br>
 * @since 1.0 <br>
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Override
    public PageInfo<Dish> search(Integer pageNum, Integer pageSize, int shopId, String dishName) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Dish> pageInfo=new PageInfo<>(dishMapper.selectBySearchVO(shopId, dishName));
        return pageInfo;
    }
    @Override
    public void add(Dish dish) throws NameException {
        if (this.dishMapper.selectByNameAndShop(dish.getDishShopId(), dish.getDishName()) != null) {
            throw  new NameException();
        }
        dish.setDishDetail("");
        dish.setDishEvaluate(0);
        dish.setDishMonth(3000);
        int ret= dishMapper.insert(dish);
        if(ret==0){
            throw new BaseBusinessException("插入菜品失败");
        }
    }
    @Override
    public Dish searchById(Integer id) {
        return dishMapper.selectById(id);
    }
    @Override
    public void modify(DishDto dishDto) throws NameException {
        if (!dishDto.getSrcDishName().equals(dishDto.getDishName())
                ||dishDto.getDishShopId()!=dishDto.getSrcDishShopId()) {
            if (dishMapper.selectByNameAndShop(dishDto.getDishShopId(), dishDto.getDishName()) != null) {
                throw  new NameException();
            }
        }
        dishMapper.update(dishDto);
    }

    @Override
    public void deleteById(Integer id) {
        int ret = dishMapper.deleteById(id);
        if (ret == 0) {
            throw new BaseBusinessException("删除不成功");
        }
    }
    @Override
    public void deleteByIds(Integer[] ids) {
        int count = dishMapper.deleteByIds(ids);
        if (count <= 0) {
            throw  new BaseBusinessException("批量上传失败");
        }
    }
}
